<?php
/*
 * 运营参数设置控制器
 */

namespace App\Http\Controllers\Admin;

use App\Models\AdminAppraisal;
use App\Models\AdminParam;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class AdminParamsController extends AdminController
{

    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth:api');
    }

    // 获取基本参数
    public function getBasic(AdminParam $model)
    {
        $basic = $model->first();
        return $this->success($basic);
    }

    // 基本参数设置
    public function saveBasic(Request $request, AdminParam $model)
    {
        $request_data = $request->all();
        $params = $model::find($request_data['id']);
        $params->site_name = $request_data['site_name'];
        $params->store_name = $request_data['store_name'];
        $params->hotline = $request_data['hotline'];
        $params->save();

        if ($params) return $this->message('操作成功');
        return $this->failed('内部错误');
    }

    // 获取运营参数
    public function getOperate(AdminParam $model)
    {
        $basic = $model->first();
        $appraisal_price = DB::table('admin_appraisals')->where('status', 1)->get();
        foreach ($appraisal_price as $price) {
            $price->index = $price->id;
        }
        $basic->appraisal_price = $appraisal_price;
        return $this->success($basic);
    }

    // 运营参数设置
    public function saveOperate(Request $request, AdminAppraisal $adminAppraisal)
    {
        $request_data = $request->all();
        $basic = DB::table('admin_params')
            ->where('id', $request_data['id'])
            ->update([
                'coupon'          => $request_data['coupon'],
                'valid_day'       => $request_data['valid_day'],
                'diagnosis_price' => $request_data['diagnosis_price'],
            ]);
        $add_data = [];   // 添加的数据
        $upd_data = [];   // 添加的数据
        $del_ids = [];    // 删除的id
        foreach ($request_data['appraisalPrice']['items'] as $k => $v) {
            // 1是更新的记录
            if ($v['status'] == 1 && !isset($v['id'])) {
                $add_data[$k]['price'] = $v['price'];
                $add_data[$k]['low_price'] = $v['low_price'];
                $add_data[$k]['high_price'] = $v['high_price'];
            }
            // 1是更新的记录
            if ($v['status'] == 1 && isset($v['id'])) {
                $upd_data[$k]['id'] = $v['id'];
                $upd_data[$k]['price'] = $v['price'];
                $upd_data[$k]['low_price'] = $v['low_price'];
                $upd_data[$k]['high_price'] = $v['high_price'];
            }
            // 0是删除的记录
            if ($v['status'] == 0 && isset($v['id'])) {
                $del_ids[] = $v['id'];
            }
        }
        // 批量添加
        if ($add_data) {
            foreach ($add_data as $v) {
                // 如果不存在时创建
                $adminAppraisal::firstOrCreate([
                    "price"      => $v['price'],
                    "low_price"  => $v['low_price'],
                    "high_price" => $v['high_price']
                ]);
            }
        }
        // 批量更新
        if ($upd_data) {
            $upd_Batch = $adminAppraisal::updateBatch('admin_appraisals', $upd_data);
        }
        // 批量删除
        if ($del_ids) {
            $del = $adminAppraisal::destroy($del_ids);
        }
        if ($basic || $add_data || $upd_data || $del_ids) return $this->message('操作成功');
        return $this->failed('内部错误');
    }
}
